cat("\f")
rm(list=ls())
options(scipen = 999)

library(dplyr)  #adattáblákkal való műveletekhez
library(ggplot2) #vizualizációkhoz
library(writexl) #xlsx-be kiírása táblázatoknak
library(extrafont) #betűtípusok betöltése
font_import()
loadfonts(device = "win")

set.seed(123)

#betöltöm a szükséges fájlokat:

ctable <- readRDS("data/ctable.RDS")
df <- readRDS("data/full_df3.RDS")

names(ctable)[names(ctable) == "id"] <- "ctable_nearest"

df <- left_join(df, select(ctable, c(ctable_nearest, color, secondary_hexcode, primary_hexcode)),by = join_by(ctable_nearest))

names(df)

df <- df %>% select(-c(km, cluster_hex_code, wdist, color))


#####################
#össsszevont számok:#
#####################


ctable <- readRDS("data/ctable.RDS")

names(df)

km <- readRDS("data/km/km14.RDS")


df$km <- km$cluster


df$km[df$km %in% c("5", "12", "10")] <- "5-12-10"

df$km[df$km %in% c("3", "14", "13")] <- "3-14-13"

df$km[df$km %in% c("6", "9", "4", "11")] <- "6-9-4-11"

df$km[df$km %in% c("7", "8")] <- "7-8"



color_summary <- df %>% group_by(km) %>% summarise(R = mean(R),
                                                   G = mean(G),
                                                   B = mean(B))


color_summary <- color_summary %>% mutate(cluster_hex_code = rgb(red = R,
                                                                 green = G,
                                                                 blue = B,
                                                                 maxColorValue = 255))


df <- left_join(df, select(color_summary, c(km, cluster_hex_code)), by = join_by(km))



for (i in 1:nrow(ctable)) {
  
  eval(parse(text = paste0(
    "color_summary <- color_summary %>% mutate(", ctable$id[i], "= (R - unlist(ctable[i,'R']))**2
                      + (G - unlist(ctable[i, 'G']))**2
                      + (B - unlist(ctable[i,'B']))**2)"
    
  )))
  print(i)
}


df_dists <- color_summary[,which(names(color_summary) %in% ctable$id)] %>% as.matrix()

color_summary <- color_summary[,which(names(color_summary) %in% ctable$id == F)]

color_summary$ctable <- apply(df_dists, 1, function(row) {
  colnames(df_dists)[which.min(row)]
})

names(ctable)[names(ctable) == "id"] <- "ctable"

color_summary <- left_join(color_summary, select(ctable, c(ctable, color)), by = join_by(ctable))

color_summary <- color_summary %>% select(-c(R,G,B,ctable))

names(color_summary)[2] <- "km_color"

df <- left_join(df, color_summary, by = join_by(km))

names(df)

summary <- summarise(group_by(df, km), arány = n()/nrow(df))

summary <- left_join(summary, color_summary, by = join_by(km))


write_xlsx(summary, "data/closest_10/kmösszevont_10arány.xlsx")
